package whu.cachhe.makeorder.ui;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.MyLocationStyle;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


import whu.cachhe.makeorder.R;
import whu.cachhe.makeorder.util.Logger;

/**
 * Created by CocoAdapter on 2018/3/27.
 */

public class ReportScanActivity extends AppCompatActivity {
    private AMap aMap;
    private RelativeLayout rlScan;

    private static final int REQUEST_SCAN_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
        initMap(savedInstanceState);
    }

    private void initMap(Bundle savedInstanceState){
        MapView mapView = findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState);
        if (aMap == null)
            aMap = mapView.getMap();

        MyLocationStyle myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
//        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
        myLocationStyle.interval(2000);
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示，非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
//                TODO
            }
        });
        aMap.moveCamera(CameraUpdateFactory.zoomTo(13));
    }

    private void init(){
        setContentView(R.layout.activity_report_scan);

        rlScan = findViewById(R.id.rl_scan_report);
        rlScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator integrator = new IntentIntegrator(ReportScanActivity.this);
                integrator.setCaptureActivity(ScanActivity.class);
                integrator.setRequestCode(REQUEST_SCAN_CODE);
                integrator.setPrompt("");
                integrator.setBeepEnabled(false);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
                integrator.setOrientationLocked(true);
                integrator.initiateScan();

                // test
//                Intent intent = new Intent(ReportScanActivity.this, ReportActivity.class);
//                intent.putExtra("lotId", "d491ba20-2681-11e8-ae1d-00163e104758");
//                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Logger.e(requestCode + ", " + resultCode);
        switch (requestCode) {
            case REQUEST_SCAN_CODE:
                IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
                String lotId = result.getContents();
                if (!TextUtils.isEmpty(lotId)){
                    Logger.e("content: " + lotId);
                    // 拿到了ID
//                    TODO 是否是合法的 停车位ID 还是说 放入report Activity 界面

                    Intent intent = new Intent(ReportScanActivity.this, ReportActivity.class);
                    intent.putExtra("lotId", lotId);
                    startActivity(intent);
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
